{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from time import perf_counter\n",
    "from itertools import combinations"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## algorithm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def timeit(fn, fargs, n_range, seconds=5):\n",
    "    print(f'[timeit] {seconds} seconds per N')\n",
    "   \n",
    "    # timeit for N\n",
    "    bench = []\n",
    "    for n in n_range:\n",
    "        args = fargs(n)\n",
    "        calls = 0\n",
    "\n",
    "        # benchmark\n",
    "        timer = perf_counter()\n",
    "        while perf_counter() - timer < seconds:\n",
    "            fn(args)\n",
    "            calls += 1\n",
    "        timer = perf_counter() - timer\n",
    "\n",
    "        # results\n",
    "        bench.append([np.e, n, timer / calls])\n",
    "        print(f'[N={n}] {calls / timer:.2f} calls/sec')\n",
    "\n",
    "    # estimate complexity\n",
    "    bench = np.log(bench)\n",
    "    (alpha, beta), *_ = np.linalg.lstsq(bench[:, :2], bench[:, -1])\n",
    "    print(f'estimated O({np.exp(alpha):.3} * N ^ {beta:.3f})')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## setup"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def combinatorial_sort(data):\n",
    "    data = data.copy()\n",
    "    for i, j in combinations(range(len(data)), 2):\n",
    "        if data[i] > data[j]:\n",
    "            data[i], data[j] = data[j], data[i]\n",
    "    return data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def get_array(n):\n",
    "    return np.random.randint(0, n, n)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## built-in sorted"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[timeit] 5 seconds per N\n",
      "[N=100] 44502.77 calls/sec\n",
      "[N=1000] 3092.61 calls/sec\n",
      "[N=10000] 231.72 calls/sec\n",
      "[N=100000] 16.99 calls/sec\n",
      "[N=1000000] 1.06 calls/sec\n",
      "estimated O(1.11e-07 * N ^ 1.151)\n"
     ]
    }
   ],
   "source": [
    "n_range = [100, 1000, 10000, 100000, 1000000]\n",
    "timeit(sorted, get_array, n_range)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## numpy sort"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[timeit] 5 seconds per N\n",
      "[N=100] 304622.14 calls/sec\n",
      "[N=1000] 55807.77 calls/sec\n",
      "[N=10000] 1966.49 calls/sec\n",
      "[N=100000] 164.66 calls/sec\n",
      "[N=1000000] 13.92 calls/sec\n",
      "estimated O(1.38e-08 * N ^ 1.121)\n"
     ]
    }
   ],
   "source": [
    "n_range = [100, 1000, 10000, 100000, 1000000]\n",
    "timeit(np.sort, get_array, n_range)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## combinatorial sort"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[timeit] 5 seconds per N\n",
      "[N=10] 49694.04 calls/sec\n",
      "[N=50] 2011.77 calls/sec\n",
      "[N=100] 515.40 calls/sec\n",
      "[N=500] 19.56 calls/sec\n",
      "[N=1000] 4.74 calls/sec\n",
      "estimated O(1.92e-07 * N ^ 2.010)\n"
     ]
    }
   ],
   "source": [
    "n_range = [10, 50, 100, 500, 1000]\n",
    "timeit(combinatorial_sort, get_array, n_range)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
